
#include "./mysql.h"
#include <stdio.h>


#define  HOST  "localhost"
#define  USER  "xq"
#define  PASSWD  "123456"
#define  DB      "test"



static  MYSQL  mysql;




//成功返回0, 失败返回-1
int my_init()
{
	//初始化句柄
    if(NULL == mysql_init(&mysql))
	{
		printf("%s\n", mysql_error(&mysql));
		return -1;
	}

	//连接数据库
	if(NULL == mysql_real_connect(&mysql, HOST, USER, PASSWD, DB, 0, NULL, 0))
	{
		printf("%s\n", mysql_error(&mysql));
		return -1;
	}
	mysql_set_character_set(&mysql, "utf8");
	return 0;
}


int my_go(char *sql, MYSQL_RES **pres)
{
	if(0!= mysql_query(&mysql, sql))
	{
		printf("%s\n", mysql_error(&mysql));
		return -1;
	}

    MYSQL_RES  *result = NULL;
	result = mysql_store_result(&mysql);
	if(result != NULL)
	{
		if(pres != NULL)
		{
			*pres = result;
		}
	}
	return 0;
}


void my_close()
{
	mysql_close(&mysql);
}
